//
//  SKDiscoverSearchView.swift
//  WeiboProjectYL
//
//  Created by yanli on 16/7/12.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit

class SKDiscoverSearchView: UIView , UITextFieldDelegate {

    // textField右边的约束
    @IBOutlet weak var textFieldRight: NSLayoutConstraint!
    // 取消按钮 属性
    @IBOutlet weak var cancelButton: UIButton!
    // textField
    @IBOutlet weak var textField: UITextField!
    
    
    //加载 xib  通过类函数
    class func searchView() -> SKDiscoverSearchView {
        //加载xib,并且初始化
        return UINib(nibName: "SKDiscoverSearchView", bundle: nil).instantiateWithOwner(nil, options: nil).last! as! SKDiscoverSearchView
    }
    
    
    
    // 取消按钮方法
    @IBAction func cancleBtnAction(sender: AnyObject) {
        //取消第一响应者
//        self.endEditing(true)
        textField.resignFirstResponder()
        //改变 textField 右边的约束 为0
        textFieldRight.constant = 0
        
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            //重新布局
            self.layoutIfNeeded()
            
            }, completion: nil)
        
    }

    // MARK: -- UITextFieldDelegate 代理
    // 开始编辑
    func textFieldDidBeginEditing(textField: UITextField) {
        // 重设约束
        textFieldRight.constant = cancelButton.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            // 重新布局
            self.layoutIfNeeded()
        }
    }

}
